import matplotlib.pyplot as plt
import pandas as pd
import calendar
from datetime import datetime
pd.options.mode.chained_assignment=None

df=pd.read_csv('train.csv')
from sklearn.model_selection import train_test_split
train_df,test_df=train_test_split(df,test_size=0.7)

test_df['traintest']='test'
train_df['traintest']='train'

all_df=pd.concat((train_df,test_df))
#2011-01-01 01:00:00
#摘出日期
all_df['date']=all_df['datetime'].apply(
    lambda x:x.split()[0]
)
print(all_df['date'])
#摘出月份
all_df['month']=all_df['date'].apply(
    lambda x:x.split('-')[1] )
#画图 每个月租赁量

# all_df['month'].value_counts().sort_index().plot(kind='line')
# plt.show()
#每月第几天
all_df['daynum']=all_df['date'].apply(
    lambda x:x.split('-')[2]
)
#每天第几个小时
all_df['hour']=all_df['datetime'].apply(
    lambda x:x.split()[1].split(':')[0]
)

# print(all_df.head())
#周几
all_df['weekday']=all_df['date'].apply(
    lambda x:calendar.day_name[datetime.strptime(x,'%Y-%m-%d').weekday()]
)
